package com.zp.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.zp.common.core.constant.UserConstants;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.entity.SysOrg;
import com.zp.system.entity.bo.SysOrgBo;
import com.zp.system.entity.vo.SysOrgVo;
import com.zp.system.service.ISysOrgService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 机构控制器
 * 前端访问路由地址为:/system/SysOrg
 *
 * @author zhang peng
 * @date 2022-12-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/SysOrg")
public class SysOrgController extends BaseController {

    private final ISysOrgService iSysOrgService;

    /**
     * 查询机构列表 (分页)
     */
//    @SaCheckPermission("system:SysOrg:list")
    @GetMapping("/selectPage")
    public TableDataInfo<SysOrgVo> selectPage (SysOrgBo bo, PageQuery pageQuery) {
        return iSysOrgService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询机构列表 (不分页)
     */
//    @SaCheckPermission("system:SysOrg:list")
    @GetMapping("/list")
    public R<List<SysOrg>> list (SysOrg sysOrg) {
        List<SysOrg> list = iSysOrgService.queryList(sysOrg);
        return R.ok(list);
    }

    /**
     * 获取机构下拉树列表
     */
    @GetMapping("/treeSelect")
    public R<List<Tree<String>>> treeselect (SysOrg sysOrg) {
        List<SysOrg> orgList = iSysOrgService.queryList(sysOrg);
        return R.ok(iSysOrgService.buildOrgTreeSelect(orgList));
    }


    /**
     * 获取机构详细信息
     *
     * @param orgId 主键
     */
    @SaCheckPermission("system:SysOrg:query")
    @GetMapping("/{orgId}")
    public R<SysOrgVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String orgId) {
        return R.ok(iSysOrgService.queryById(orgId));
    }

    /**
     * 新增机构
     */
    @SaCheckPermission("system:SysOrg:add")
    @Log(title = "机构管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody SysOrgBo bo) {
        if (UserConstants.NOT_UNIQUE.equals(iSysOrgService.checkOrgNameUnique(bo))) {
            return R.fail("新增机构'" + bo.getOrgName() + "'失败，机构名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(iSysOrgService.checkOrgCodeUnique(bo))) {
            return R.fail("新增机构'" + bo.getOrgCode() + "'失败，机构代码已存在");
        }
        return toAjax(iSysOrgService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改机构
     */
    @SaCheckPermission("system:SysOrg:edit")
    @Log(title = "机构管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody SysOrgBo bo) {
        if (UserConstants.NOT_UNIQUE.equals(iSysOrgService.checkOrgNameUnique(bo))) {
            return R.fail("修改机构'" + bo.getOrgName() + "'失败，机构名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(iSysOrgService.checkOrgCodeUnique(bo))) {
            return R.fail("修改机构'" + bo.getOrgCode() + "'失败，机构代码已存在");
        }
        return toAjax(iSysOrgService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除机构
     *
     * @param orgIds 主键串
     */
    @SaCheckPermission("system:SysOrg:remove")
    @Log(title = "机构管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orgIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] orgIds) {
        return toAjax(iSysOrgService.deleteWithValidByIds(Arrays.asList(orgIds), true) ? 1 : 0);
    }


    /**
     * 状态修改
     */
    @SaCheckPermission("system:SysOrg:edit")
    @Log(title = "机构管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus (@RequestBody SysOrg sysOrg) {
        return toAjax(iSysOrgService.updateOrgStatus(sysOrg));
    }
}
